package com.xuanwei.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xuanwei.core.entity.param.UserAddress;
import com.xuanwei.core.exception.XwException;
import com.xuanwei.core.service.IXwUserAddressService;
import com.xuanwei.dao.entity.XwUserAddress;
import com.xuanwei.dao.mapper.XwUserAddressMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2019-07-11
 */
@Service
public class XwUserAddressServiceImpl extends ServiceImpl<XwUserAddressMapper, XwUserAddress> implements IXwUserAddressService {

    @Override
    @Transactional
    public void add(UserAddress userAddress, Integer userId) {
        //默认地址
        XwUserAddress address = baseMapper.selectOne(new QueryWrapper<XwUserAddress>()
                .select("id")
                .eq("default_flag", 1));

        XwUserAddress xwUserAddress = new XwUserAddress();
        BeanUtils.copyProperties(userAddress, xwUserAddress);
        if (userAddress.getDefaultFlag() == null) {
            xwUserAddress.setDefaultFlag(0);
        }
        xwUserAddress.setUserId(userId);
        xwUserAddress.setUpdateTime(LocalDateTime.now());
        xwUserAddress.setCreateTime(LocalDateTime.now());
        save(xwUserAddress);
        //将默认的设置为非默认的
        if (xwUserAddress.getDefaultFlag() == 1) {
            if (address != null) {
                address.setDefaultFlag(0);
                address.setUpdateTime(LocalDateTime.now());
                updateById(address);
            }
        }
    }


    @Override
    @Transactional
    public void update(UserAddress userAddress) {
        if (userAddress.getId() == null) {
            throw new XwException("ID不能为空");
        }
        //默认地址
        XwUserAddress address = baseMapper.selectOne(new QueryWrapper<XwUserAddress>()
                .select("id")
                .eq("default_flag", 1));

        XwUserAddress xwUserAddress = baseMapper.selectOne(new QueryWrapper<XwUserAddress>()
                .select("id,default_flag")
                .eq("id", userAddress.getId()));
        if (xwUserAddress == null) {
            throw new XwException("数据不存在");
        }
        Integer defaultAddress = xwUserAddress.getDefaultFlag();
        //修改
        BeanUtils.copyProperties(userAddress, xwUserAddress);
        xwUserAddress.setUpdateTime(LocalDateTime.now());
        updateById(xwUserAddress);
        //修改的地址为默认并且之前状态不是默认
        if (xwUserAddress.getDefaultFlag() == 1 && defaultAddress != 1) {

            if (address != null) {
                address.setDefaultFlag(0);
                address.setUpdateTime(LocalDateTime.now());
                updateById(address);
            }
        }
    }

    @Override
    public List<UserAddress> findByUserId(Integer userId) {

        List<XwUserAddress> userAddresses = list(new QueryWrapper<XwUserAddress>()
                .select("id,province,city,area,receive_name,receive_phone,receive_address,default_flag")
                .eq("user_id", userId));
        List<UserAddress> list = Lists.newArrayListWithCapacity(userAddresses.size());
        userAddresses.forEach(s -> {
            UserAddress userAddress = new UserAddress();
            BeanUtils.copyProperties(s, userAddress);
            list.add(userAddress);
        });
        return list;
    }

    @Override
    public void delete(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public UserAddress findById(Integer id, Integer userId) {
        XwUserAddress xwUserAddress = null;
        if (id == null) {
            xwUserAddress = baseMapper.selectOne(
                    new QueryWrapper<XwUserAddress>()
                            .eq("user_id", userId)
                            .eq("default_flag", 1));
            if (xwUserAddress == null) {
                return null;
            }

        } else {
            xwUserAddress = baseMapper.selectById(id);
            if (xwUserAddress == null) {
                throw new XwException("数据不存在");
            }
        }

        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(xwUserAddress, userAddress);
        return userAddress;
    }
}
